package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.CommentSign;
import cn.wolfcode.trip.base.domain.Information;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.CommentSignMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ICommentSignService;
import cn.wolfcode.trip.base.service.IInformationService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommentSignServiceImpl implements ICommentSignService {
    @Autowired
    private CommentSignMapper commentSignMapper;
    @Autowired
    private IInformationService informationService;


    @Override
    public CommentSign get(Long id) {
        return commentSignMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CommentSign> listAll() {
        return commentSignMapper.selectAll();
    }

    @Override
    public void save(CommentSign commentSign) {
        //设置点赞或者收藏的时间
        if (commentSign.getGreatStatus() != null) {
            commentSign.setGreatTime(new Date());
        }
        if (commentSign.getEnshrineStatus() != null) {
            commentSign.setEnshrineTime(new Date());
        }
        //获取被点赞或者收藏的作者的id
        commentSignMapper.insert(commentSign);
        /**
         * 保存信息,和用户信息的关系
         */
        saveInfoRela(commentSign);
    }

    @Override
    public void update(CommentSign commentSign) {
        //设置点赞或者收藏的时间
        if (commentSign.getGreatStatus() != null) {
            commentSign.setGreatTime(new Date());
        }
        if (commentSign.getEnshrineStatus() != null) {
            commentSign.setEnshrineTime(new Date());
        }
        //获取被点赞或者收藏的作者的id
        commentSignMapper.updateByPrimaryKey(commentSign);
        saveInfoRela(commentSign);
    }

    /**
     * 保存用户的点赞以及收藏信息
     *
     * @param commentSign
     */
    private void saveInfoRela(CommentSign commentSign) {
        User currentUser = UserContext.getCurrentUser();
        Information information = new Information();
        User author = commentSign.getAuthor();
        if (commentSign.getType() == 0 && commentSign.getGreatStatus()) {
            information.setUser(currentUser);
            information.setType(Information.USER_MESSAGE_LIKE);
            information.setTitle("评论");
            information.setContent(currentUser.getNickName() + "给您的评论点赞了");
            informationService.saveUserInformation(information, author);
        }
        if (commentSign.getType() == 1 && commentSign.getEnshrineStatus()) {
            information.setUser(currentUser);
            information.setType(Information.USER_MESSAGE_COLLECTION);
            information.setTitle("评论");
            information.setContent(currentUser.getNickName() + "收藏了您的评论");
            informationService.saveUserInformation(information, author);
        }

    }

    @Override
    public PageInfo queryForList(QueryObject qo) {
        return null;
    }

    @Override
    public void updateState(CommentSign commentSign) {

    }


    /**
     * 根据大攻略id查询当前用户的在该点评下的标记状态
     *
     * @param commentId
     * @return
     */
    @Override
    public CommentSign selectByCommentId(Long commentId, Long userId) {

        return commentSignMapper.selectByCommentId(commentId, userId);
    }

    @Override
    public int queryGreatStatusCountByCommentId(Long commentId) {

        return commentSignMapper.queryGreatStatusCountByCommentId(commentId);
    }

    @Override
    public int queryEnshrineStatusCountByCommentId(Long commentId) {
        return commentSignMapper.queryEnshrineStatusCountByCommentId(commentId);
    }


}
